local Label = require "widgets/hx_label"
local t_util = require "libs/tableutil"
local h_util = {}

-- 使得不是全局对齐的ui可以拖动，func_stop用来执行拖动结束位置的事件
function h_util:ActivateUIDraggable(UI, func_stop)
    UI.FollowMouse = function(self)
        if self.followhandler == nil then
            local cur_pos = TheInput:GetScreenPosition()
            local scale = 1 / self.parent:GetScale().x
            local ori_pos = self:GetPosition()
            self.followhandler = TheInput:AddMoveHandler(function(x, y)
                self:SetPosition((Vector3(x, y, 0) - cur_pos) * scale + ori_pos)
            end)
        end
    end

    local _OnMouseButton = UI.OnMouseButton
    UI.OnMouseButton = function(ui, press, down, ...)
        local result = _OnMouseButton(ui, press, down, ...)
        if ui.frame.focus then
            if press == MOUSEBUTTON_LEFT then
                if down then
                    return ui:FollowMouse()
                elseif type(func_stop) == "function" then
                    func_stop(ui:GetPosition())
                end
            end
        end
        ui:StopFollowMouse()
        return result
    end
end

function h_util:CreateLabel(target, text, offset, font, size, colour)
    return ThePlayer and ThePlayer.HUD and ThePlayer.HUD:AddChild(Label(target, text, offset, font, size, colour))
end

local minimap_path = "minimap/minimap_data.xml"
local function checkAtlas(xml, tex)
    xml = xml and resolvefilepath(xml) or xml
    return xml and TheSim:AtlasContains(xml, tex) and xml
end
local xml_path = {resolvefilepath("images/customicobyysh.xml"), resolvefilepath("images/shroomcake_moreicons.xml"),
                  resolvefilepath("images/qwert.xml")}
-- TODO: 显示调味料理
function h_util:GetPrefabAsset(prefab)
    local tex = prefab .. ".tex"
    local name = STRINGS.NAMES[string.upper(prefab)] or prefab
    local xml = GetInventoryItemAtlas(tex) 
    local atlas = checkAtlas(xml, tex)
    if atlas then
        return atlas, tex, name
    end
    for _, path in ipairs(xml_path) do
        atlas = checkAtlas(path, tex)
        if atlas then
            return atlas, tex, name
        end
    end
    local p_data = Prefabs[prefab]
    if p_data then
        local p_assets = p_data.assets
        for _, asset in ipairs(p_assets) do 
            local alt, img = xml, tex
            if asset.type == "INV_IMAGE" then
                img = asset.file .. '.tex'
                alt = GetInventoryItemAtlas(img)
            elseif asset.type == "ATLAS" then
                alt = asset.file
            elseif asset.type == "IMAGE" then
                img = asset.file
                img = string.reverse(img:reverse():sub(1, string.find(img:reverse(), "/")-1))	
                alt = t_util:GetElement(p_assets, function(_, asset)
                    return asset.type == "ATLAS" and asset.file
                end)
            end
            atlas = checkAtlas(alt, img)
            if atlas then
                return atlas, img, name
            elseif asset.type == "INV_IMAGE" then
                img = 'quagmire_' .. asset.file .. '.tex'
                alt = GetInventoryItemAtlas(img)
                atlas = checkAtlas(alt, img)
                if atlas then
                    return atlas, img, name
                end
            end
        end
    end

    local png = prefab .. ".png"
    atlas = checkAtlas(minimap_path, png)
    if atlas then
        return atlas, png, name
    else
        atlas = checkAtlas(GetScrapbookIconAtlas(tex), tex)
    end
    return atlas, atlas and tex, name
end

return h_util
